home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK2.toast / Development Kits (Disc 2) / QuickDraw GX / Programming Stuff / Sample Code / Graphics Samples / ShapePart Browser ƒ / ShapeAction.c < prev    next >
Encoding:
Text File  |  1996-04-15  |  8.6 KB  |  364 lines  |  [TEXT/KAHL]

  1. /*
  2.  *    ShapeAction.c
  3.  *
  4.  *    Robert Dierkes,  April 26, 1993
  5.  *
  6.  *    Change History:
  7.  *
  8.  *       4/93    ???        New
  9.  *       4/96    bob        Updated #includes to support changed GX Library names.
  10.  *                    Added the copyright info.
  11.  *
  12.  *
  13.  *        © Apple Computer, Inc. 1990 - 1996  All rights reserved
  14.  *
  15.  */
  16.  
  17.  
  18. #undef    MAC_HEADERS
  19.  
  20.  
  21. /*------------------*/
  22. /*    Include Files    */
  23. /*------------------*/
  24. #ifndef    MAC_HEADERS
  25.     #include <Dialogs.h>
  26.     #include <Controls.h>
  27. #endif    MAC_HEADERS
  28.  
  29. #define debugging                /* for gxGraphicNotices */
  30.  
  31. #include <GXGraphics.h>
  32. #include "GraphicsLibraries.h"
  33. #include "QDLibrary.h"            /* ShortRectToFixed for DrawGrayBox */
  34.  
  35. #include "ResourceIDs.h"
  36. #include "ShapeAction.h"
  37. #include "ShapeSetup.h"
  38.  
  39.  
  40. /*----------------------*/
  41. /*    Global Declarations    */
  42. /*----------------------*/
  43.  
  44.  
  45. /*------------------------------*/
  46. /*    External Declarations     */
  47. /*------------------------------*/
  48. extern    GlobalStructure    globals;
  49.  
  50.  
  51. /*------------------------------*/
  52. /*       Local ProtoTypes       */
  53. /*------------------------------*/
  54. void ShowLocalBounds (gxShape *p1stShape, long shapeCount);
  55. void HighlightShapePartBoxes (ControlHandle *ph1stControl, gxShapePart partsHit);
  56. long GetShapeBoxIndexFromPoint (gxShape boxes, gxPoint *pWhere);
  57. void UpdateShapeName (gxShape theShape, gxShapeType *pLastType);
  58. pascal void DrawGrayBox (WindowPtr pWindow, short itemNum);
  59.  
  60.  
  61.     void
  62. ShowLocalBounds (gxShape *p1stShape, long shapeCount)
  63. {
  64.     register
  65.     gxShape        *pShape,
  66.                 rectShape;
  67.     gxRectangle    bounds;
  68.  
  69.     pShape = p1stShape + shapeCount - 1;
  70.  
  71.     rectShape = GXNewShape (gxRectangleType);
  72.     GXSetShapeFill (rectShape, gxClosedFrameFill);
  73.     SetShapeCommonColor (rectShape, gxGray);
  74.  
  75.     while (shapeCount--)
  76.     {
  77.         GXGetShapeLocalBounds (*pShape--, &bounds);
  78.         GXSetRectangle (rectShape, &bounds);
  79.         GXDrawShape (rectShape);
  80.     }
  81.  
  82.     GXDisposeShape (rectShape);
  83. }
  84.  
  85.  
  86.     void
  87. ShowControlPoints (gxShape *p1stShape, long shapeCount)
  88. {
  89.     register
  90.     gxShape        *pShape;
  91.     long        contourCount,
  92.                 pointCount;
  93.     gxPoint        pt;
  94.     gxShape        pointShape;
  95.     gxRectangle    bounds;
  96.  
  97.     pShape = p1stShape + shapeCount - 1;
  98.     pointShape = GXNewShape (gxRectangleType);
  99.  
  100.     while (shapeCount--)
  101.     {
  102.         /* gxGlyphType has too many control points so skip it */
  103.         if (GXGetShapeType (*pShape) != gxGlyphType)
  104.         {
  105.             contourCount = GXCountShapeContours (*pShape);
  106.             while (contourCount--)
  107.             {
  108.                 pointCount = GXGetShapePoints (*pShape, 1, gxSelectToEnd, nil);
  109.                 while (pointCount)
  110.                 {
  111. ///                    GetShapeIndexPoint (*pShape, pointCount--, &pt);
  112.                     GXGetShapePoints (*pShape, pointCount--, 1, &pt);
  113.                     bounds.left   = pt.x - fixed1;
  114.                     bounds.top    = pt.y - fixed1;
  115.                     bounds.right  = pt.x + fixed1;
  116.                     bounds.bottom = pt.y + fixed1;
  117.                     GXSetRectangle (pointShape, &bounds);
  118.                     GXDrawShape (pointShape);
  119.                 }
  120.             }
  121.         }
  122.         pShape--;
  123.     }
  124.  
  125.     GXDisposeShape (pointShape);
  126. }
  127.  
  128.  
  129. /*
  130.  *
  131.  */
  132.      void
  133. HighlightShapePartBoxes (ControlHandle *ph1stControl, gxShapePart partsHit)
  134. {
  135.     #define    controlCount    11
  136.  
  137.     ControlHandle    *pControl;
  138.     gxRectangle        controlRect;
  139.     gxShape            highlight;
  140.     static
  141.     gxShapePart        lastPartsHit = gxAnyPart;
  142.     gxShapePart        partMask;
  143.  
  144.     if (partsHit == lastPartsHit)
  145.         return;
  146.  
  147.     highlight = GXNewShape (gxRectangleType);
  148.     GXSetShapeFill (highlight, gxClosedFrameFill);
  149.     GXIgnoreGraphicsNotice (color_already_set);
  150.  
  151.     partMask = gxPatternPart;
  152.     for (pControl = ph1stControl + controlCount; --pControl >= ph1stControl; partMask >>= 1)
  153.     {
  154.         /* Compare single bit of current shapeParts against previous shapeParts */
  155.         if ((partsHit & partMask) != (lastPartsHit & partMask))
  156.         {
  157.             (void) ShortRectToFixed (&(**pControl)->contrlRect, &controlRect);
  158.             controlRect.left   -= fixed1;
  159.             controlRect.top    -= fixed1;
  160.             controlRect.right  += fixed1;
  161.  
  162.             GXSetRectangle (highlight, &controlRect);
  163.             SetShapeCommonColor (highlight, (partsHit & partMask) ? red : gxWhite);
  164.             GXDrawShape (highlight);
  165.         }
  166.     }
  167.     lastPartsHit = partsHit;
  168.  
  169.     GXPopGraphicsNotice ();
  170.     GXDisposeShape (highlight);
  171. }
  172.  
  173.  
  174.     pascal void
  175. DrawGrayBox (WindowPtr pWindow, short itemNum)
  176. {
  177.     GrafPtr        savedPort;
  178.     Rect        shortBounds;
  179.     gxRectangle    bounds;
  180.     gxShape        boxShape;
  181.     short        itemType;
  182.     Handle        itemHandle;
  183.  
  184.     GetPort (&savedPort);
  185.     SetPort (pWindow);
  186.  
  187.     GetDItem (pWindow, itemNum, &itemType, &itemHandle, &shortBounds);
  188.     ShortRectToFixed (&shortBounds, &bounds);
  189.     boxShape = GXNewRectangle (&bounds);
  190.     GXSetShapeFill (boxShape, gxClosedFrameFill);
  191.     GXSetShapePen (boxShape, 4 * fixed1);
  192.     SetShapeCommonColor (boxShape, grayish + silver);
  193.     GXDrawShape (boxShape);
  194.     GXDisposeShape (boxShape);
  195.  
  196.     SetPort (savedPort);
  197.  
  198. }
  199.  
  200.  
  201.     void
  202. UpdateHitTestWindow (WindowPtr pWindow)
  203. {
  204.     register
  205.     gxShape        *pShape;
  206.     long        shapeCount,
  207.                 shapeCountDown;
  208.  
  209.     UpdtDialog (pWindow, pWindow->visRgn);
  210.  
  211.     DrawGrayBox (pWindow, userGrayShapeNameBox);
  212.     DrawGrayBox (pWindow, userGrayShapePartsBox);
  213.     HighlightShapePartBoxes (&globals.hBounds, globals.partsHit);
  214.  
  215.     shapeCount = shapeCountDown = GXCountShapeContours (globals.boxes);
  216.  
  217.     /* Draw the background boxes */
  218.     GXDrawShape (globals.boxes);
  219.  
  220.     if (globals.showLocalBounds)
  221.         ShowLocalBounds (globals.pShapes, shapeCount);
  222.  
  223.     /* Draw all the hit testing shapes */
  224.     pShape = globals.pShapes;
  225.     while (shapeCountDown--)
  226.         GXDrawShape (*pShape++);
  227.  
  228.     if (globals.showControlPoints)
  229.         ShowControlPoints (globals.pShapes, shapeCount);
  230. }
  231.  
  232.  
  233.     long
  234. GetShapeBoxIndexFromPoint (gxShape boxes, gxPoint *pWhere)
  235. {
  236.     gxRectangle    bounds;
  237.     long        index;
  238.  
  239.     index = GXCountShapeContours (boxes);
  240.     while (index)
  241.     {
  242.         GXGetShapeBounds (boxes, index, &bounds);
  243.         if (GXTouchesRectanglePoint (&bounds, pWhere))
  244.             return (index - 1);
  245.         index--;
  246.     }
  247.     return (notOnAnyShape);
  248. }
  249.  
  250.  
  251.     void
  252. UpdateShapeName (gxShape theShape, gxShapeType *pLastType)
  253. {
  254.     gxShapeType    newType;
  255.     Handle        itemHandle;
  256.     Str255        shapeNames[] = {"\pPoint",
  257.                                 "\pLine",
  258.                                 "\pCurve",
  259.                                 "\pRectangle",
  260.                                 "\pPolygon",
  261.                                 "\pPath",
  262.                                 "\pBitmap",
  263.                                 "\pText",
  264.                                 "\pGlyph",
  265.                                 "\p"
  266.                                 };
  267.     StringPtr    pShapeName;
  268.     short        itemType;
  269.     Rect        itemRect;
  270.  
  271.     if (theShape == nil)
  272.         newType = gxEmptyType;
  273.     else if ((newType = GXGetShapeType (theShape)) == *pLastType)
  274.         return;
  275.  
  276.     *pLastType = newType;
  277.  
  278.     switch (newType)
  279.     {
  280.         case gxPointType:        pShapeName = shapeNames [0];    break;
  281.         case gxLineType:        pShapeName = shapeNames [1];    break;
  282.         case gxCurveType:        pShapeName = shapeNames [2];    break;
  283.         case gxRectangleType:    pShapeName = shapeNames [3];    break;
  284.         case gxPolygonType:        pShapeName = shapeNames [4];    break;
  285.         case gxPathType:        pShapeName = shapeNames [5];    break;
  286.         case gxBitmapType:        pShapeName = shapeNames [6];    break;
  287.         case gxTextType:        pShapeName = shapeNames [7];    break;
  288.         case gxGlyphType:        pShapeName = shapeNames [8];    break;
  289.         default:                pShapeName = shapeNames [9];    break;
  290.     }
  291.  
  292.     GetDItem (qd.thePort, staticShapeName, &itemType, &itemHandle, &itemRect);
  293.     SetIText (itemHandle, pShapeName);
  294. }
  295.  
  296.  
  297. /*
  298.     Draws a red rectangle around check box if the given point hit that type of gxShape part.
  299. */
  300.     void
  301. UpdateShapePartInfo (Point *pQDWhere, GlobalStructure *pG)
  302. {
  303.     gxPoint            where;
  304.     gxHitTestInfo    hitInfo;
  305.     long            boxIndex;
  306.     static
  307.     gxShapeType        lastType = gxEmptyType;
  308.     gxShape            shapeHit;
  309.  
  310.     ShortPointToFixed (pQDWhere, &where);
  311.     boxIndex = GetShapeBoxIndexFromPoint (pG->boxes, &where);
  312.  
  313.     /* Change checkbox titles if point is in glyph gxShape */
  314.     if (boxIndex == kWordGlyphs  &&  lastType != gxGlyphType)
  315.     {
  316.         Point    joinSize     = {16, 106},
  317.                 startCapSize = {16, 90},
  318.                 endCapSize     = {16, 88},
  319.                 dashSize     = {16, 100};
  320.  
  321.         SizeControl (pG->hJoin, joinSize.h, joinSize.v);
  322.         SizeControl (pG->hStartCap, startCapSize.h, startCapSize.v);
  323.         SizeControl (pG->hEndCap, endCapSize.h, endCapSize.v);
  324.         SizeControl (pG->hDash, dashSize.h, dashSize.v);
  325.         SetCTitle (pG->hJoin,      "\pGlyph Bounds");
  326.         SetCTitle (pG->hStartCap, "\pGlyph First");
  327.         SetCTitle (pG->hEndCap,   "\pGlyph Last");
  328.         SetCTitle (pG->hDash,      "\pSide Bearing");
  329.     }
  330.     else if (boxIndex != kWordGlyphs  &&  lastType == gxGlyphType)
  331.     {
  332.         Point    joinSize     = {16, 48},
  333.                 startCapSize = {16, 82},
  334.                 endCapSize     = {16, 72},
  335.                 dashSize     = {16, 52};
  336.         Rect    invalid         = {274, 528, 354, 588};
  337.  
  338.         InvalRect (&invalid);
  339.         SizeControl (pG->hJoin, joinSize.h, joinSize.v);
  340.         SizeControl (pG->hStartCap, startCapSize.h, startCapSize.v);
  341.         SizeControl (pG->hEndCap, endCapSize.h, endCapSize.v);
  342.         SizeControl (pG->hDash, dashSize.h, dashSize.v);
  343.         SetCTitle (pG->hJoin,      "\pJoin");
  344.         SetCTitle (pG->hStartCap, "\pStart Cap");
  345.         SetCTitle (pG->hEndCap,      "\pEnd Cap");
  346.         SetCTitle (pG->hDash,      "\pDash");
  347.     }
  348.  
  349.     if (boxIndex == notOnAnyShape)
  350.     {
  351.         shapeHit = nil;
  352.         hitInfo.what = gxNoPart;
  353.     }
  354.     else
  355.     {
  356.         shapeHit = pG->pShapes [boxIndex];
  357.         GXHitTestShape (pG->pShapes [boxIndex], &where, &hitInfo);
  358.     }
  359.  
  360.     UpdateShapeName (shapeHit, &lastType);
  361.     HighlightShapePartBoxes (&pG->hBounds, hitInfo.what);
  362.     pG->partsHit = hitInfo.what;
  363. }
  364.